package cn.zn.modules.zzsys.enums.po;

import cn.zn.modules.zzsys.enums.ProcessGroupTypeEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author ZN
 */
@Data
@Slf4j
public class ProcessGroupPermissions {
    private Boolean hasYzbgh;
    private Boolean hasJjb;
    private Boolean hasRsb;
    private Boolean hasJfzcshz;
    private Boolean hasZxxz;

    public Boolean canSetup(ProcessGroupTypeEnum e) {
        try {
            String fieldName = "has".concat(e.getCode().substring(0, 1).toUpperCase()).concat(e.getCode().substring(1));
            Field[] fields = this.getClass().getDeclaredFields();
            boolean hasField = false;
            for (Field field : fields) {
                if(field.getName().equals(fieldName)){
                    hasField = true;
                    break;
                }
            }
            if(!hasField){
                return null;
            }
            Method method = this.getClass().getMethod("getHas".concat(e.getCode().substring(0, 1).toUpperCase()).concat(e.getCode().substring(1)), null);
            if (method == null) {
                return null;
            }
            Object invoke = method.invoke(this);
            return (Boolean) invoke;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException exception) {
            log.error("获取或执行方法失败：{}", exception.getMessage(), exception);
            return false;
        }
    }
}
